from rest_framework import serializers


from tsl_backend.frontend_api.models.workorder import ProductionActualBOM, Detail, ProductionStandardBOM
import tsl_backend.frontend_api.models.order_routing as order_routing
import tsl_backend.users.models as users
import logging
# from tsl_backend.frontend_api.models.route import Master
class StandardBomSerializer(serializers.ModelSerializer):

    class Meta:
        model = ProductionStandardBOM
        fields = '__all__'

    def create(self, validated_data):

        print(validated_data)

        instance, created = ProductionStandardBOM.objects.update_or_create(
            workorder=validated_data.pop('workorder'), article=validated_data.pop('article'),
            defaults={
                'article_description': validated_data.pop('article_description'),
                'qty': validated_data.pop('qty'),
                'weight': validated_data.pop('weight'),
                'unit': validated_data.pop('unit'),
                'is_main_stone': validated_data.pop('is_main_stone'),
                'stone_type': validated_data.pop('stone_type'),
                'is_stone': validated_data.pop('is_stone')
                      })
        
        # print(instance, created)

        return instance


class DetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = Detail
        # fields = ['order', 'description', 'due_date', 'quantity', 'goods_type', 'ring_type', 'gold_type', 'site', 'ready_date', 'pos', 'design', 'mould', 'remark', 'effect', 'product_mark_content', 'product_mark_description', 'product_mark_font', 'master', 'updated_due_date', 'black_pic_url', 'ProdStandardBOMs']
        exclude = ['gold_print_date', 'stone_print_date', 'state']


    def create(self, validated_data):
        instance, created = Detail.objects.update_or_create(
            order=validated_data.get('order'), 
            defaults={
                'description': validated_data.get('description'),
                'due_date': validated_data.get('due_date'),
                'quantity': validated_data.get('quantity'),
                'goods_type': validated_data.get('goods_type'),
                'ring_type': validated_data.get('ring_type'),
                'gold_type': validated_data.get('gold_type'),
                'site': validated_data.get('site'),
                'ready_date': validated_data.get('ready_date'),
                'pos': validated_data.get('pos'),
                'design': validated_data.get('design'),
                'mould': validated_data.get('mould'),
                'remark': validated_data.get('remark'),
                'effect': validated_data.get('effect'),
                'product_mark_content': validated_data.get('product_mark_content'),
                'product_mark_description': validated_data.get('product_mark_description'),
                'product_mark_font': validated_data.get('product_mark_font'),
                'master': validated_data.get('master'),
                'updated_due_date': validated_data.get('updated_due_date'),
                'black_pic_url': validated_data.get('black_pic_url'),
                'source': validated_data.get('source'),
                'flower_count': validated_data.get('flower_count'),
                'ori_fg_article': validated_data.get('ori_fg_article'),
                      })
        
        # print(instance, created)

        return instance


class ActualBomSerializer(serializers.ModelSerializer):
    qty = serializers.CharField(source='quantity')
    seqNo = serializers.IntegerField(source='seq')
    isMainStone = serializers.BooleanField(source='is_main_stone')
    laserMark = serializers.CharField(source='laser_mark', allow_null=True, allow_blank=True)
    stoneColor = serializers.CharField(source='stone_color', allow_null=True, allow_blank=True)
    stoneClarity = serializers.CharField(source='stone_clarity', allow_null=True, allow_blank=True)
    stoneCert = serializers.CharField(source='stone_cert', allow_null=True, allow_blank=True)

    class Meta:
        model = ProductionActualBOM
        fields = '__all__'

    # def create(self, validated_data):

    #     logging.info(f"Insert actual bom data: {validated_data=}")

    #     instance, created = ProductionActualBOM.objects.update_or_create(
    #         workorder=validated_data.get('orderNo'), article=validated_data.get('article'),seq=validated_data.get('seqNo'),
    #         defaults={
    #             'quantity': validated_data.get('qty'),
    #             'weight': validated_data.get('weight'),
    #             'unit': validated_data.get('unit'),
    #             'is_main_stone': validated_data.get('isMainStone'),
    #             'laser_mark': validated_data.get('laserMark'),
    #             'stone_color': validated_data.get('stoneColor'),
    #             'stone_clarity': validated_data.get('stoneClarity'),
    #             'stone_cert': validated_data.get('stoneCert'),
    #                   })
        
    #     # print(instance, created)

    #     return instance

class FlowSerializer(serializers.ModelSerializer):
    class Meta:
        model = users.Flow
        fields = ["name"]
        
class RouteDetailSerializer(serializers.ModelSerializer):
    flow = FlowSerializer()
    status = serializers.SerializerMethodField()
    step = serializers.SerializerMethodField()
    class Meta:
        model = order_routing.RouteDetail
        fields = ["flow", "consumption_percentage", "worker_fee", "step", "status"]

    def to_representation(self, instance):
        ret = super().to_representation(instance)
        ret['flow'] = ret['flow']['name']
        return ret
    
    def get_step(self, obj):
        return obj.seq

    def get_status(self,obj):
        if obj.status == 1:
            return 'open'
        if obj.status == 2:
            return 'in progress'
        if obj.status == 3:
            return 'complete'



class DiscardDetailSerializer(serializers.ModelSerializer):

    class Meta:
        model = Detail
        fields = ["state"]

